package com.itboye.mybatis.datasource;

import com.itboye.system.helper.ByConsoleHelper;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.reflect.Method;

/**
 * DataSourceAspect.java
 * Created on  2016/8/11 16:07
 * 版本       修改时间          作者      修改内容
 * V1.0.1    2016/8/11      hebidu    初始版本
 * <p>
 * Copyright (c) 2013 杭州博也网络科技有限公司 版权所有
 * HANGZHOU BOYE TECHNOLOGY CO.,LTD. All Rights Reserved.
 */
public class ByDataSourceAspect {

     public void before(JoinPoint point) {
            Object target = point.getTarget();
            String method = point.getSignature().getName();

            Class<?>[] classz = target.getClass().getInterfaces();

            Class<?>[] parameterTypes = ((MethodSignature) point.getSignature())
                    .getMethod().getParameterTypes();
            try {
                Method m = classz[0].getMethod(method, parameterTypes);
                if (m != null && m.isAnnotationPresent(ByDataSource.class)) {
                    ByDataSource data = m
                            .getAnnotation(ByDataSource.class);
                    ByConsoleHelper.debug("ByDataSource = "+data.value(),null);
                    DynamicDataSource.setDataSourceKey(data.value());
                }

            } catch (Exception e) {
                ByConsoleHelper.info(e.getLocalizedMessage(),null);
                e.printStackTrace();
            }
        }

}
